﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class eduEditDocumentsForCourse : System.Web.UI.Page
{
    DataClassesDataContext SMSDC = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            //读入CourseID,写出Course名称
            int m_CourseID = Convert.ToInt32(this.Request.QueryString["CourseID"]);
            this.Label1.Text = (from a in SMSDC.CourseInfos
                                where a.CourseID == m_CourseID
                                select a.CourseName).Single().ToString().Trim();
            BindDocuments();
        }
    }

    private void BindDocuments()
    {
        ChbDocumentList.DataSource = this.SqlDataSource1;
        ChbDocumentList.DataTextField = "CourDocumentName";
        ChbDocumentList.DataValueField = "CourDocumentID";
        ChbDocumentList.DataBind();
        int m_CourseID = Convert.ToInt32(this.Request.QueryString["CourseID"]);
        var h = (from a in SMSDC.DocumentsInCourses
                 where a.CourseID == m_CourseID
                 select a.CourDocumentID).ToList();

        foreach (int CourDocumentID in h)
            ChbDocumentList.Items.FindByValue(CourDocumentID.ToString()).Selected = true;
    }

    //更新
    protected void BtnUpdate_Click(object sender, EventArgs e)
    {
        int m_CourseID = Convert.ToInt32(this.Request.QueryString["CourseID"]);
        var currDocuments = (SMSDC.Edu_GetDocumentsForCourse(m_CourseID)).ToList();
        if (currDocuments.Count > 0)
            SMSDC.Edu_RemoveCourseFromDocuments(m_CourseID);

        foreach (ListItem item in ChbDocumentList.Items)
        {
            if (item.Selected)
                SMSDC.Edu_AddCourseToDocuments(m_CourseID, Convert.ToInt32(item.Value));
        }
        this.RegisterStartupScript("message", "<script language='javascript' defer>alert('已经成功更新！');</script>");
    }
}